//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "InterpolatingCPICapFloorEngine.h"
using namespace Cephei::QL::Experimental::Inflation;
#include <gen/QL/Experimental/Inflation/CPICapFloorTermPriceSurface.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Inflation::CInterpolatingCPICapFloorEngine::CInterpolatingCPICapFloorEngine (Cephei::QL::Experimental::Inflation::ICPICapFloorTermPriceSurface^ priceSurf) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CCPICapFloorTermPriceSurface^ _CpriceSurf;
    try
    {
#ifdef HANDLE
        _phInterpolatingCPICapFloorEngine = NULL;
#endif
        _CpriceSurf = safe_cast<CCPICapFloorTermPriceSurface^> (priceSurf);
        _CpriceSurf->Lock();
        Handle<QuantLib::CPICapFloorTermPriceSurface>& _priceSurf = static_cast<Handle<QuantLib::CPICapFloorTermPriceSurface>&> (_CpriceSurf->GetHandle ()); 
        _ppInterpolatingCPICapFloorEngine = new boost::shared_ptr<QuantLib::InterpolatingCPICapFloorEngine> (new QuantLib::InterpolatingCPICapFloorEngine ( _priceSurf ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CpriceSurf != nullptr) _CpriceSurf->Unlock();
    }
}
Cephei::QL::Experimental::Inflation::CInterpolatingCPICapFloorEngine::CInterpolatingCPICapFloorEngine (boost::shared_ptr<QuantLib::InterpolatingCPICapFloorEngine>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phInterpolatingCPICapFloorEngine = NULL;
#endif
	_ppInterpolatingCPICapFloorEngine = &childNative;
    
}
Cephei::QL::Experimental::Inflation::CInterpolatingCPICapFloorEngine::CInterpolatingCPICapFloorEngine (QuantLib::InterpolatingCPICapFloorEngine& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phInterpolatingCPICapFloorEngine = NULL;
#endif
	_ppInterpolatingCPICapFloorEngine = new boost::shared_ptr<QuantLib::InterpolatingCPICapFloorEngine> (&childNative);
    
    _InterpolatingCPICapFloorEngineOwner = owner;
    
}

Cephei::QL::Experimental::Inflation::CInterpolatingCPICapFloorEngine::CInterpolatingCPICapFloorEngine (CInterpolatingCPICapFloorEngine^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phInterpolatingCPICapFloorEngine = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppInterpolatingCPICapFloorEngine = new boost::shared_ptr<QuantLib::InterpolatingCPICapFloorEngine> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Inflation::CInterpolatingCPICapFloorEngine::CInterpolatingCPICapFloorEngine (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phInterpolatingCPICapFloorEngine = NULL;
#endif
	if (!t->IsSubclassOf(CInterpolatingCPICapFloorEngine::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Inflation::CInterpolatingCPICapFloorEngine::CInterpolatingCPICapFloorEngine (QuantLib::Handle<QuantLib::InterpolatingCPICapFloorEngine>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phInterpolatingCPICapFloorEngine = &childNative;
	_ppInterpolatingCPICapFloorEngine = &static_cast<boost::shared_ptr<QuantLib::InterpolatingCPICapFloorEngine>>(childNative.currentLink());
    
    _InterpolatingCPICapFloorEngineOwner = owner;
}
Cephei::QL::Experimental::Inflation::CInterpolatingCPICapFloorEngine::CInterpolatingCPICapFloorEngine (QuantLib::Handle<QuantLib::InterpolatingCPICapFloorEngine> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phInterpolatingCPICapFloorEngine = &childNative;
	_ppInterpolatingCPICapFloorEngine = &static_cast<boost::shared_ptr<QuantLib::InterpolatingCPICapFloorEngine>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Inflation::CInterpolatingCPICapFloorEngine::CInterpolatingCPICapFloorEngine (QuantLib::InterpolatingCPICapFloorEngine childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phInterpolatingCPICapFloorEngine = NULL;
#endif
	_ppInterpolatingCPICapFloorEngine = new boost::shared_ptr<QuantLib::InterpolatingCPICapFloorEngine> (new QuantLib::InterpolatingCPICapFloorEngine (childNative));
    
}
#endif

Cephei::QL::Experimental::Inflation::CInterpolatingCPICapFloorEngine::~CInterpolatingCPICapFloorEngine ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppInterpolatingCPICapFloorEngine != NULL)
    {
	    delete _ppInterpolatingCPICapFloorEngine;
        _ppInterpolatingCPICapFloorEngine = NULL;
    }
}
Cephei::QL::Experimental::Inflation::CInterpolatingCPICapFloorEngine::!CInterpolatingCPICapFloorEngine ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppInterpolatingCPICapFloorEngine != NULL)
    {
	    delete _ppInterpolatingCPICapFloorEngine;
    }
}
QuantLib::InterpolatingCPICapFloorEngine& Cephei::QL::Experimental::Inflation::CInterpolatingCPICapFloorEngine::GetReference ()
{
    if (_ppInterpolatingCPICapFloorEngine == NULL) throw REFNEW NativeNullException ();
	return **_ppInterpolatingCPICapFloorEngine;
}
boost::shared_ptr<QuantLib::InterpolatingCPICapFloorEngine>& Cephei::QL::Experimental::Inflation::CInterpolatingCPICapFloorEngine::GetShared ()
{
    if (_ppInterpolatingCPICapFloorEngine == NULL) throw REFNEW NativeNullException ();
	return *_ppInterpolatingCPICapFloorEngine;
}
QuantLib::InterpolatingCPICapFloorEngine* Cephei::QL::Experimental::Inflation::CInterpolatingCPICapFloorEngine::GetPointer ()
{
    if (_ppInterpolatingCPICapFloorEngine == NULL) throw REFNEW NativeNullException ();
	return &**_ppInterpolatingCPICapFloorEngine;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::InterpolatingCPICapFloorEngine>& Cephei::QL::Experimental::Inflation::CInterpolatingCPICapFloorEngine::GetHandle ()
{
	if (_phInterpolatingCPICapFloorEngine == NULL)
	{
		_phInterpolatingCPICapFloorEngine = new Handle<QuantLib::InterpolatingCPICapFloorEngine> (*_ppInterpolatingCPICapFloorEngine);
	}
	return *_phInterpolatingCPICapFloorEngine;
}
#endif
bool Cephei::QL::Experimental::Inflation::CInterpolatingCPICapFloorEngine::HasNative () 
{
	return (_ppInterpolatingCPICapFloorEngine != NULL);
}

Cephei::QL::Experimental::Inflation::IInterpolatingCPICapFloorEngine^ Cephei::QL::Experimental::Inflation::CInterpolatingCPICapFloorEngine::Calculate::get ()
{
    try
    {
    	(*_ppInterpolatingCPICapFloorEngine)->calculate ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
String^ Cephei::QL::Experimental::Inflation::CInterpolatingCPICapFloorEngine::Name::get ()
{
    try
    {
    	std::string _rv = (std::string)(*_ppInterpolatingCPICapFloorEngine)->name ( );   
        String^ _nrv = (String^)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Inflation::IInterpolatingCPICapFloorEngine^ Cephei::QL::Experimental::Inflation::CInterpolatingCPICapFloorEngine_Factory::Create (Cephei::QL::Experimental::Inflation::ICPICapFloorTermPriceSurface^ priceSurf)
{
    return REFNEW CInterpolatingCPICapFloorEngine ( priceSurf);
}
